package com.bst.bsj.portal.util;
import net.sourceforge.pinyin4j.PinyinHelper;  
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
public class Pinyin {
	  /** 
     * 灏嗘眽瀛楄浆鎹负鍏ㄦ嫾 
     *  
     * @param src 
     * @return String 
     */  
    public static String getPinYin(String src) {  
        char[] t1 = null;  
        t1 = src.toCharArray();  
        // System.out.println(t1.length);  
        String[] t2 = new String[t1.length];  
        // System.out.println(t2.length);  
        // 璁剧疆姹夊瓧鎷奸煶杈撳嚭鐨勬牸寮� 
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
        String t4 = "";  
        int t0 = t1.length;  
        try {  
            for (int i = 0; i < t0; i++) {  
                // 鍒ゆ柇鏄惁涓烘眽瀛楀瓧绗� 
                // System.out.println(t1[i]);  
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {  
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 灏嗘眽瀛楃殑鍑犵鍏ㄦ嫾閮藉瓨鍒皌2鏁扮粍涓� 
                    t4 += t2[0];// 鍙栧嚭璇ユ眽瀛楀叏鎷肩殑绗竴绉嶈闊冲苟杩炴帴鍒板瓧绗︿覆t4鍚� 
                } else {  
                    // 濡傛灉涓嶆槸姹夊瓧瀛楃锛岀洿鎺ュ彇鍑哄瓧绗﹀苟杩炴帴鍒板瓧绗︿覆t4鍚� 
                    t4 += Character.toString(t1[i]);  
                }  
            }  
        } catch (BadHanyuPinyinOutputFormatCombination e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return t4;  
    }  
  
    /** 
     * 鎻愬彇姣忎釜姹夊瓧鐨勯瀛楁瘝 
     *  
     * @param str 
     * @return String 
     */  
    public static String getPinYinHeadChar(String str) {  
        String convert = "";  
        for (int j = 0; j < str.length(); j++) {  
            char word = str.charAt(j);  
            // 鎻愬彇姹夊瓧鐨勯瀛楁瘝  
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
            if (pinyinArray != null) {  
                convert += pinyinArray[0].charAt(0);  
            } else {  
                convert += word;  
            }  
        }  
        return convert;  
    }  
  
    /** 
     * 灏嗗瓧绗︿覆杞崲鎴怉SCII鐮�
     *  
     * @param cnStr 
     * @return String 
     */  
    public static String getCnASCII(String cnStr) {  
        StringBuffer strBuf = new StringBuffer();  
         byte[] bGBK = cnStr.getBytes();  
        for (int i = 0; i < bGBK.length; i++) {  
            // System.out.println(Integer.toHexString(bGBK[i] & 0xff));  
             strBuf.append(Integer.toHexString(bGBK[i] & 0xff)+" ");  
        }  
        return strBuf.toString();  
    }  
  
    public static void main(String[] args) {  
        String cnStr = "楝树".substring(0,1);  
   
        System.out.println(getPinYinHeadChar(cnStr).toUpperCase());  
//      System.out.println(getCnASCII(cnStr));  
    }  
  
}
